/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * bfs好做，直接返回每一层的最右侧的值
 * @param {TreeNode} root
 * @return {number[]}
 */
var rightSideView = function (root) {
    if (root === null) return []
    let stack = [root],
        ans = [];

    while (stack.length > 0) {
        let len = stack.length;

        ans.push(stack[len - 1].val);
        for (let i = 0; i < len; i++) {
            let node = stack.shift()
            node.left && stack.push(node.left)
            node.right && stack.push(node.right)
        }
    }
    return ans;
};